package com.ruoyi.experiment.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.experiment.mapper.ExperimentMapper;
import com.ruoyi.experiment.domain.Experiment;
import com.ruoyi.experiment.service.IExperimentService;

/**
 * 实验管理Service业务层处理
 * 
 * @author gw
 * @date 2025-01-08
 */
@Service
public class ExperimentServiceImpl implements IExperimentService 
{
    @Autowired
    private ExperimentMapper experimentMapper;

    /**
     * 查询实验管理
     * 
     * @param experimentId 实验管理主键
     * @return 实验管理
     */
    @Override
    public Experiment selectExperimentByExperimentId(Long experimentId)
    {
        return experimentMapper.selectExperimentByExperimentId(experimentId);
    }

    /**
     * 查询实验管理列表
     * 
     * @param experiment 实验管理
     * @return 实验管理
     */
    @Override
    public List<Experiment> selectExperimentList(Experiment experiment)
    {
        return experimentMapper.selectExperimentList(experiment);
    }

    /**
     * 新增实验管理
     * 
     * @param experiment 实验管理
     * @return 结果
     */
    @Override
    public int insertExperiment(Experiment experiment)
    {
        experiment.setCreateTime(DateUtils.getNowDate());
        experiment.setCreateBy(SecurityUtils.getUsername());
        return experimentMapper.insertExperiment(experiment);
    }

    /**
     * 修改实验管理
     * 
     * @param experiment 实验管理
     * @return 结果
     */
    @Override
    public int updateExperiment(Experiment experiment)
    {
        experiment.setUpdateTime(DateUtils.getNowDate());
        experiment.setUpdateBy(SecurityUtils.getUsername());
        return experimentMapper.updateExperiment(experiment);
    }

    /**
     * 批量删除实验管理
     * 
     * @param experimentIds 需要删除的实验管理主键
     * @return 结果
     */
    @Override
    public int deleteExperimentByExperimentIds(Long[] experimentIds)
    {
        int deletedCount = 0;
        for (Long experimentId : experimentIds) {
            Experiment experiment = experimentMapper.selectExperimentByExperimentId(experimentId);
            experiment.setDeleteBy(SecurityUtils.getUsername());
            experiment.setDeleteTime(DateUtils.getNowDate());
            experiment.setDelFlag(1);
            experimentMapper.updateExperiment(experiment);
            deletedCount++;
        }
        return deletedCount;
    }

    /**
     * 删除实验管理信息
     * 
     * @param experimentId 实验管理主键
     * @return 结果
     */
    @Override
    public int deleteExperimentByExperimentId(Long experimentId)
    {
        Experiment experiment = experimentMapper.selectExperimentByExperimentId(experimentId);
        experiment.setDeleteBy(SecurityUtils.getUsername());
        experiment.setDeleteTime(DateUtils.getNowDate());
        experiment.setDelFlag(1);
        return experimentMapper.updateExperiment(experiment);
    }
}
